﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using System.Linq;

namespace CloudFranceTriple.Tests.Areas.Forum
{
    [TestClass]
    public class AnnonceControllerTest : UnitTestBase
    {
        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_ShowOnHeader()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.ShowOnHeader() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_ShowOnPortail()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.ShowOnPortail() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_ShowOnPost()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.ShowOnPost() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_ShowOnTopic()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.ShowOnTopic() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_ShowOnForum()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.ShowOnForum() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Index()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Index() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Create_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Create_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var forumannonceset = new ForumAnnonceSet
            {
                DateBegin = new DateTime(_now.Year, _now.Month, _now.Day),
                DateEnd = new DateTime(_now.Year, _now.Month, _now.Day).AddDays(1),
                Order = 0,
                ShowOnForum = false,
                ShowOnHeader = false,
                ShowOnPortail = false,
                ShowOnPost = false,
                ShowOnTopic = false,
                Text = "<h1>==>^^ Vive les tests unitaires !!!</h1>"
            };

            // Act
            var result = controller.Create(forumannonceset) as RedirectToRouteResult;


            UnitTestAnnonceId = forumannonceset.Id;

            // Assert
            Assert.IsNotNull(result);
        }


        //[TestMethod]
        //[TestCategory("Annonce")]
        //TODO : AnnonceController_Create_Post_ModelHasError
        public void AnnonceController_Create_Post_ModelHasError()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;

            var forumannonceset = new ForumAnnonceSet();

            // Act
            var result = controller.Create(forumannonceset) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Edit_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Edit(UnitTestAnnonceId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Edit_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var forumannonceset = new ForumAnnonceSet
            {
                Id = UnitTestAnnonceId,
                DateBegin = new DateTime(_now.Year, _now.Month, _now.Day),
                DateEnd = new DateTime(_now.Year, _now.Month, _now.Day).AddDays(1),
                Order = 10,
                ShowOnForum = false,
                ShowOnHeader = false,
                ShowOnPortail = false,
                ShowOnPost = false,
                ShowOnTopic = false,
                Text = "<h1>==>^^ Vive les tests unitaires -- EDIT !!!</h1>"
            };

            // Act
            var result = controller.Edit(forumannonceset) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_Delete_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Delete(UnitTestAnnonceId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Annonce")]
        public void AnnonceController_DeleteConfirmed_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.AnnonceController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.DeleteConfirmed(UnitTestAnnonceId) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }
    }
}
